package demo4.controller;

import com.tedu.InputUtil;
import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import demo4.pojo.UserDelete;
import demo4.pojo.UserLogin;
import demo4.pojo.UserUpdate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

/**
 * Controller 控制器，是MVC中的C(后面学习MVC是会详细介绍)
 * 这个层级的类主要职责:与客户端交互，接收客户端请求，并转交给后面业务层处理，并根据
 * 业务层处理的结果回馈给用户。
 *
 * 为了简化目前的操作，我们在当前案例中让Controller也处理业务的工作(省略业务层)
 */
@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        System.out.println("请选择对应的操作，输入序号:");
        System.out.println("1:注册,2:登录,3:列表,4:修改,5:删除,6:再说");
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        switch (num){
            case 1:
                regUser();
                start();
                break;
            case 2:
                loginUser();
                start();
                break;
            case 3:
                listUser();
                start();
                break;
            case 4:
                updateUser();
                start();
                break;
            case 5:
                deleteUser();
                start();
                break;
            default:
                System.out.println("其他功能还在完善中，目前404");
        }
    }

    private void deleteUser() {
        // 先登录用户信息，然后删除当前用户的信息
        System.out.println("请先登录");
        UserLogin login = InputUtil.getInputObject(new UserLogin());
        User query = new User();
        query.setUsername(login.getUsername());
        query.setPassword(login.getPassword());
        List<User> users = userMapper.select(query);
        if (users != null && users.size() == 1) {
            User user = users.get(0);
            UserDelete delete = InputUtil.getInputObject(new UserDelete());
            if (delete.getFlag() != null && delete.getFlag() == 1) {
                Integer num = userMapper.delete(user);
                System.out.println(num > 0 ? "删除成功" : "删除失败");
            } else {
                System.out.println("取消删除");
            }
        } else {
            System.out.println("登录失败");
        }
    }

    private void updateUser() {
        // 先登录用户信息，然后修改用户输入的信息
        System.out.println("请先登录");
        UserLogin login = InputUtil.getInputObject(new UserLogin());
        User query = new User();
        query.setUsername(login.getUsername());
        query.setPassword(login.getPassword());
        List<User> users = userMapper.select(query);
        if (users != null && users.size() == 1) {
            User user = users.get(0);
            System.out.println("请录入要修改的用户信息字段");
            UserUpdate update = InputUtil.getInputObject(new UserUpdate());
            // 如果update中的字段不为空，则将user的值改为update对象的值
            if (StringUtils.isNotBlank(update.getUsername())) {
                user.setUsername(update.getUsername());
            }
            if (StringUtils.isNotBlank(update.getPassword())) {
                user.setPassword(update.getPassword());
            }
            if (StringUtils.isNotBlank(update.getNickname())) {
                user.setNickname(update.getNickname());
            }
            if (update.getAge() != null) {
                user.setAge(update.getAge());
            }
            Integer num = userMapper.update(user);
            System.out.println(num > 0 ? "修改成功" : "修改失败");
        } else {
            System.out.println("登录失败");
        }
    }

    private void listUser() {
        System.out.println("开始处理用户列表");
        List<User> users = userMapper.select(new User());
        if (users != null) {
            users.forEach(user -> System.out.println(user));
        } else {
            System.out.println("用户列表为空");
        }
    }

    public void regUser(){
        System.out.println("开始处理用户注册");
        /*
            1:获取用户输入的注册信息
            2:将信息插入到数据库中
         */
        User user = InputUtil.getInputObject(new User());
        User query = new User();
        query.setUsername(user.getUsername());
        List<User> users = userMapper.select(query);
        if (users != null && users.size() > 0) {
            System.out.println("已存在用户，id为" + users.get(0).getId());
        } else {
            userMapper.insert(user);
        }
    }

    public void loginUser(){
        System.out.println("开始处理用户登录");
        UserLogin login = InputUtil.getInputObject(new UserLogin());
        User query = new User();
        query.setUsername(login.getUsername());
        query.setPassword(login.getPassword());
        List<User> users = userMapper.select(query);
        if (users != null && users.size() > 0) {
            System.out.println("登录成功，id为" + users.get(0).getId());
        } else {
            System.out.println("登录失败");
        }
    }
}









